#encoding: utf-8

import requests
import json

# 发送问题记录程序钉钉消息
def sendDingTalkMsg_record(toUrl, orderCode, messageUrl, issueDesc, issueLever, provider, atWho=[]):
    headers = {'Content-Type': 'application/json'}
    msg = {
        "msgtype": "markdown",
        "markdown": {
            "title": "问题反馈",
            "text": "#### [" + orderCode + "](" + messageUrl + ") \n> "
                    "##### 问题描述：" + issueDesc + " \n> "
                    "###### 严重程度：" + issueLever + " \n> "
                    "###### 反馈人：" + provider + " \n"
        },
        "at": {
            "atMobiles": atWho,
            "isAtAll": False
        }
    }
    msg = json.JSONEncoder().encode(msg)
    try:
        res = requests.post(toUrl, data=msg, headers=headers)
        result = json.loads(res.text)
    except Exception as e:
        result = {"code":-9,"message":str(e), "data":{}}
    return result


# 发送sparrow程序钉钉报警消息
def sendDingTalkMsg_sparrow(toUrl, summary, msgText, atWho=[]):
    summary = str(summary)
    headers = {'Content-Type': 'application/json'}
    msg = {
        "msgtype": "markdown",
        "markdown": {
            "title": "小麻雀接口报警",
            "text": "#### "+ summary + "，失败用例信息如下： \n> "
                    " " + msgText + " \n"
        },
        "at": {
            "atMobiles": atWho,
            "isAtAll": False
        }
    }
    msg = json.JSONEncoder().encode(msg)
    try:
        res = requests.post(toUrl, data=msg, headers=headers)
        result = json.loads(res.text)
    except Exception as e:
        result = {"code":-9,"message":str(e), "data":{}}
    return result

# 发送sparrow程序钉钉预警消息，如响应时间耗时超过n秒
def sendDingTalkWarnMsg_sparrow(toUrl, summaryMsg, msgText, atWho=[]):
    headers = {'Content-Type': 'application/json'}
    msg = {
        "msgtype": "markdown",
        "markdown": {
            "title": "接口预警",
            "text": "#### "+ summaryMsg + " \n> "
                    " " + msgText + " \n"
        },
        "at": {
            "atMobiles": atWho,
            "isAtAll": False
        }
    }
    msg = json.JSONEncoder().encode(msg)
    try:
        res = requests.post(toUrl, data=msg, headers=headers)
        result = json.loads(res.text)
    except Exception as e:
        result = {"code":-9,"message":str(e), "data":{}}
    return result



